#include "appviewimpl.h"
#include "stdafx.h"

int main_t(_pid_t pid, int argc, TCHAR* argv[])
{
    //"{51FA8DE1-216F-4A76-B4F4-B986E9F54C27}:mainview:100:4"
    //"{CE065148-7803-45B1-B1D7-874B52B8F5C5}:uibase:10:4"
    //"{99103D46-735F-44EE-A6F1-2C94DF20901E}:logs:5:4",
    //"{34F9B3BF-6F56-4058-9DCD-04D9A5F0174B}:logs:4:4",
    logi("s_strcpy_func %p", s_strcpy);
    logi("s_strcat_func %p", s_strcat);

    HRESULT hr = S_OK;

    int iRes = S_SUCCESS;

    basic_tchar path[MAX_PATH + 1] = {0x00};
    get_executepath(pid_null, path, MAX_PATH);
    size_t plen = tstring_strlen(path);

    const char* strguid = "{409C8B8C-8467-47EE-B1BE-4E4112E64926}";
    CContainer com;
    GUID rid;
    rid = StringToGUID(strguid);

#if (TARGET_OS == OS_WIN)
    const char* code = "{\"component\":[\
	\"{7F17A42A-B5DF-4281-87C3-BFC44486F55C}:runtime:2:2\",\
	\"{F170A724-AACA-4603-BB1C-0A8EAF1C3322}:asynio:3:4\",\
	\"{C54C9CC0-F448-4A49-A622-0467D02E8EB8}:net:5:4\",\
	\"{99103D46-735F-44EE-A6F1-2C94DF20901E}:logs:11:4\",\
	\"{34F9B3BF-6F56-4058-9DCD-04D9A5F0174B}:logs:12:4\",\
	\"{201409F6-22F8-48d3-A69F-7935BDDE6BFF}:msgbus:13:4\"] }";
#else
    const char* code = "{\"component\":[ \
		\"{7F17A42A-B5DF-4281-87C3-BFC44486F55C}:runtime:2:2\",\
		\"{C54C9CC0-F448-4A49-A622-0467D02E8EB8}:net:5:4\",\
		\"{99103D46-735F-44EE-A6F1-2C94DF20901E}:logs:11:4\",\
		\"{34F9B3BF-6F56-4058-9DCD-04D9A5F0174B}:logs:12:4\",\
        \"{F170A724-AACA-4603-BB1C-0A8EAF1C3322}:asynio:3:4\"]}";
#endif

    SafePStringPtr containerpath;
    containerpath.ptr = path;
    containerpath.len = plen;

    SafeStringPtr containercode;
    containercode.ptr = code;
    containercode.len = s_strlen(code);

    logi("test_main_LoadContainer");
    hr = com.LoadContainer(&containerpath);
    rc_error(hr == S_OK, S_ERROR);

    logi("test_main_CreateContainer");
    hr = com.CreateContainer(rid, &containerpath, &containercode);
    rc_error(hr == S_OK, S_ERROR);

    logi("test_main_InitContainer");
    hr = com.InitContainer(rid, pid, argv, argc);
    rc_error(hr == S_OK, S_ERROR);

    ULONG count = com.GetCompentCount(rid);
    ULONG index = com.GetStartIndex(rid, count - 1);

    CAppViewImpl* app = ALLOC_NEW CAppViewImpl();
    _lComPtr<IBase> pBase;
    hr = app->QueryInterface(IID_IBase, (void**)&pBase);
    rc_error(hr == S_OK, S_ERROR);

#if (TARGET_OS == OS_WIN)
    index = index + 1;
#else
    index = index + 1;
#endif

    hr = com.RegisterComponent(rid, CLSID_AppView, "AppView.impl.V1", pBase, index, "appview", STD_COMPOENT | STD_INIT /*| STD_START*/);

    rc_error(hr == S_OK, S_ERROR);

    logi("test_main_RunContainer");
    hr = com.RunContainer(rid, 0);
    rc_error(hr == S_OK, S_ERROR);

    logi("test_main_InitContainer");
    hr = com.UnInitContainer(rid);
    rc_error(hr == S_OK, S_ERROR);

    logi("test_main_DestroyContainer");

    return 0;
}

#if (TARGET_OS == OS_POSIX)

int main(int argc, char* argv[])
{
    _pid_t pid = get_processid();
    return main_t(pid, argc, argv);
}

#elif (TARGET_OS == OS_MACH)

int main(int argc, char* argv[])
{
    _pid_t pid = get_processid();
    return main_t(pid, argc, argv);
}
#elif (TARGET_OS == OS_UNIX)
int main(int argc, char* argv[])
{
    _pid_t pid = get_processid();
    return main_t(pid, argc, argv);
}
#elif (TARGET_OS == OS_WIN)

// #pragma comment(lib,"Kernel32.lib")
// #pragma comment(lib,"comctl32.lib")
// #pragma comment(linker,"/subsystem:windows /entry:WinMain")

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    int iRet = 0;
    int nArgs = 0;

    _pid_t pid = get_processid();
    LPWSTR* szArglist = NULL;
    szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
    iRet = main_t(pid, nArgs, szArglist);
    rc_error(nArgs != 0, 0);
    LocalFree(szArglist);
    return 0;
}
#endif
